package com.client.model;

import com.client.controller.ChatFrameManager;
import com.client.controller.FriendListFrameManager;
import com.client.view.FriendListFrame;
import com.common.Message;
import com.client.view.ChatFrame;
import com.common.MessageType;
import com.server.controller.historyRecord;
import com.server.model.UserService;

import javax.swing.*;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Set;

/**
 * @author lxl
 * @date 2020/12/26 - 14:33
 */
/*
客户端和服务器端保持通讯的教程
 */
public class ClientSocketThread extends Thread{
    private Socket s;
    public int flag = 1;
    Message message;
    public Socket getS() {
        return s;
    }

    public ClientSocketThread(Socket s){
        this.s = s;
    }

    public void run(){
        while (true){
            if(flag==0) break;
            //不断地去读取服务器发来的数据
            try {
                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                message = (Message)ois.readObject();

                //将信息计入聊天记录中:2021/10/6/9:19
                /**
                 因为会不停地读取服务端发送来的数据，
                 所以需要筛选过滤掉部分数据，
                 只记录有效的数据
                */
                 if(message.getSender()!=null&&(message.getMesType().equals("3")||message.getMesType().equals("6"))) {
                    new historyRecord().loadRecord(message);
                }

                if(message.getMesType().equals(MessageType.message_common)||message.getMesType().equals(MessageType.message_file)
                        ||message.getMesType().equals(MessageType.message_commonToAll)||message.getMesType().equals(MessageType.message_fileToAll)
                        ||message.getMesType().equals(MessageType.message_shakeWindow) || message.getMesType().equals(MessageType.message_shakeWindowToALL)){
                    System.out.println("读取到服务器发来的消息"+message.getSender()+"给"+message.getGetter()
                            +"的"+message.getText());
                    //这里有可能是all,正好打开AndAll
                    ChatFrame chatFrame =null;
                    if(message.getMesType().equals(MessageType.message_commonToAll)||message.getMesType().equals(MessageType.message_fileToAll)){
                        chatFrame = ChatFrameManager.getQqChat( message.getGetter()+ "AndAll");
                    }else{
                        chatFrame = ChatFrameManager.getQqChat( message.getGetter()+ "And" +message.getSender());
                    }
                    //显示信息
                    if(chatFrame ==null){//有可能还没打开这个页面, 那现在给打开
                        if(message.getMesType().equals(MessageType.message_commonToAll)||message.getMesType().equals(MessageType.message_fileToAll)){
                            chatFrame = new ChatFrame(FriendListFrameManager.getQqFriendList(message.getGetter()),message.getGetter(), "All");
                            ChatFrameManager.addQqChat(message.getGetter()+"And"+"All", chatFrame);
                        }else{
                            chatFrame = new ChatFrame(FriendListFrameManager.getQqFriendList(message.getGetter()),message.getGetter(), message.getSender());
                            ChatFrameManager.addQqChat(message.getGetter()+"And"+message.getSender(), chatFrame);
                        }
                    }
                    if (message.getMesType().equals(MessageType.message_common)||message.getMesType().equals(MessageType.message_commonToAll)){
                        System.out.println("获得消息");
                        chatFrame.showMessage(message);
                    }else if(message.getMesType().equals(MessageType.message_file)||message.getMesType().equals(MessageType.message_fileToAll)){
                        chatFrame.receiveFile(message);
                    }else if(message.getMesType().equals(MessageType.message_shakeWindowToALL) || message.getMesType().equals(MessageType.message_shakeWindow)) { //窗口抖动
                        System.out.println("接收到一个抖动窗口");
                        chatFrame.showMessage(message);
                        chatFrame.ShakingWindow();
                    }
                    chatFrame.toFront();
                    chatFrame.setVisible(true);
                }else if(message.getMesType().equals(MessageType.message_ret_onLineFriend)){

                    //修改相应的好友列表
                    FriendListFrame friendListFrame = FriendListFrameManager.getQqFriendList(message.getGetter());
                    if (friendListFrame !=null)//有可能这个列表暂时还不存在
                    friendListFrame.updateFriend(message);

                    //更新在线好友
                }
            }catch (Exception e){
                if(e.getMessage().equals("Socket closed")) {
                    //下线更新服务器在线用户情况(刷新)
                    try {
                        new UserService().loadExit(message.getSender());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    System.out.println("客户端已下线!");
                }
                else if(e.getMessage().equals("Connection reset")){
                    HashMap<String, FriendListFrame> map = FriendListFrameManager.getMap();
                    Set<String> keySet = map.keySet();
                    for (String key : keySet) {
                        FriendListFrame friendListFrame = map.get(key);
                        JOptionPane.showMessageDialog(friendListFrame,"服务器关闭了","警告",JOptionPane.WARNING_MESSAGE);
                        friendListFrame.disposeAll();
                    }
                    break;
                }
                else{
                    e.printStackTrace();
                }
            }
        }
    }
}
